package com.example.shop_2304.bannerfg

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.base.mvvm.BaseFragment
import com.example.base.mvvm.BaseViewFragment
import com.example.shop_2304.R
import com.example.shop_2304.databinding.FragmentBannerBinding
import com.example.shop_2304.utils.GlideUtils
import com.example.shop_2304.xq.XqActivity
import com.example.shop_2304.xq.XqViewModel
import com.youth.banner.loader.ImageLoader


class BannerFragment : BaseFragment<FragmentBannerBinding,XqViewModel>() {
    val list = mutableListOf<String>()
    override fun initView() {
        val xqActivity = activity as XqActivity
        val gid = xqActivity.gid
        Log.d("sss", "initView: 拿到的gid为$gid")
        //请求详情
        viewModel.xq(gid,"normal")
    }

    override fun initData() {
        viewModel.xqSuccess.observe(this){
            if(it.code==200){
                //展示轮播图
                list.add(it.data.productInfo.image)
                list.add(it.data.productInfo.image)
                binding.banner.setImages(list)
                    .setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if (context != null) {
                                if (imageView != null) {
                                    GlideUtils.loadImage(context, path.toString(),imageView)
                                }
                            }
                        }
                    })
                    .start()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
    }

    override fun getLayout(): Int  = R.layout.fragment_banner

    override fun getViewModel(): Class<XqViewModel>  = XqViewModel::class.java


}